package com.weigongbao.weigongbao;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;


public class VolleyUtils {

    private static VolleyUtils mVolley;
    private final RequestQueue mQueue;

    private VolleyUtils() {
        mQueue = Volley.newRequestQueue(WeiGongBao.mContext);
    }

    public static VolleyUtils initVolley() {

        if (null == mVolley) {
            synchronized (VolleyUtils.class) {
                if (null == mVolley) {
                    mVolley = new VolleyUtils();
                }
            }
        }
        return mVolley;
    }

    public <T> void get(String url, Class clase, final CallBack<T> callBack){
        GsonRequest<T> request = new GsonRequest<>(url, clase, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callBack.fail(error.getMessage());
            }
        }, new Response.Listener<T>() {
            @Override
            public void onResponse(T response) {
                callBack.succeed(response);
            }
        });
        mQueue.add(request);
    }


}
